package com.ruoyi.project.question.compose.controller;

import java.util.List;

import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.question.compose.domain.QuestionnaireQuestion;
import com.ruoyi.project.question.compose.domain.QuestionnaireUserConn;
import com.ruoyi.project.question.compose.service.IQuestionnaireQuestionService;
import com.ruoyi.project.question.compose.service.IQuestionnaireUserConnService;
import com.ruoyi.project.question.info.domain.QuestionInfo;
import com.ruoyi.project.question.info.service.IQuestionInfoService;
import com.ruoyi.project.system.post.service.IPostService;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.question.compose.domain.QuestionnaireInfo;
import com.ruoyi.project.question.compose.service.IQuestionnaireInfoService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 考核问卷Controller
 *
 * @author Bandit
 * @date 2021-12-14
 */
@Controller
@RequestMapping("/question/compose")
public class QuestionnaireInfoController extends BaseController
{
    private String prefix = "question/compose";

    @Autowired
    private IQuestionnaireInfoService questionnaireInfoService;

    @Autowired
    private IQuestionnaireQuestionService questionnaireQuestionService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IQuestionnaireUserConnService questionnaireUserConnService;

    @Autowired
    private IQuestionInfoService questionInfoService;

    @RequiresPermissions("question:compose:view")
    @GetMapping()
    public String compose()
    {
        return prefix + "/compose";
    }

    /**
     * 查询考核问卷列表
     */
    @RequiresPermissions("question:compose:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(QuestionnaireInfo questionnaireInfo)
    {
        startPage();
        List<QuestionnaireInfo> list = questionnaireInfoService.selectQuestionnaireInfoList(questionnaireInfo);
        return getDataTable(list);
    }


    /**
     * 进入问卷分配问题页
     */
    @RequiresPermissions("system:compose:edit")
    @GetMapping("/authInfo/{questionnaireID}")
    public String authRole(@PathVariable("questionnaireID") Long questionnaireID, ModelMap mmap)
    {
        QuestionnaireInfo questionnaire = questionnaireInfoService.selectQuestionnaireInfoById(questionnaireID);
        List<QuestionInfo> infoList = questionInfoService.selectQuestionInfoByQuestionnaireID(questionnaireID);
        mmap.put("questionnaire",questionnaire);
        mmap.put("questionInfoList", infoList);
        return prefix + "/authInfo";
    }

    @RequestMapping("/handOut")
    @ResponseBody
    public AjaxResult handOut(@RequestParam("id") Long questionnaireId){
        QuestionnaireInfo questionnaireInfo = questionnaireInfoService.selectQuestionnaireInfoById(questionnaireId);
        Long postID = questionnaireInfo.getPersonType();                    //被审查人员类型
        List<User> informantList = userService.selectUsersByPostId(postID);  //被审查人列表
        System.out.println(informantList);
        List<User> userList = userService.selectUserList(new User());         //分发人的列表
        for (User user : userList) {
            if (user.getUserId().equals(ShiroUtils.getUserId())) continue;
            for (User informantUser : informantList) {
                if (informantUser.getUserId().equals(user.getUserId())) continue;
                questionnaireUserConnService.insertQuestionnaireUserConn(
                        new QuestionnaireUserConn(user.getUserId(),questionnaireId,informantUser.getUserId(),1L));
            }
        }
        return AjaxResult.success();
    }


    /**
     * 问卷分配问题确认
     */
    @RequiresPermissions("system:compose:edit")
    @Log(title = "用户管理", businessType = BusinessType.GRANT)
    @PostMapping("/authInfo/insertAuthInfo")
    @ResponseBody
    public AjaxResult insertAuthInfo(@RequestParam("questionnaireId") Long id, Long[] Ids)
    {
        questionnaireQuestionService.deleteQuestionnaireQuestionByQuestionnaireId(id);
        for (Long questionId : Ids) {
            questionnaireQuestionService.insertQuestionnaireQuestion(new QuestionnaireQuestion(id,questionId));
        }
        return success();
    }


    /**
     * 导出考核问卷列表
     */
    @RequiresPermissions("question:compose:export")
    @Log(title = "考核问卷", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(QuestionnaireInfo questionnaireInfo)
    {
        List<QuestionnaireInfo> list = questionnaireInfoService.selectQuestionnaireInfoList(questionnaireInfo);
        ExcelUtil<QuestionnaireInfo> util = new ExcelUtil<QuestionnaireInfo>(QuestionnaireInfo.class);
        return util.exportExcel(list, "考核问卷数据");
    }

    /**
     * 新增考核问卷
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存考核问卷
     */
    @RequiresPermissions("question:compose:add")
    @Log(title = "考核问卷", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(QuestionnaireInfo questionnaireInfo)
    {
        return toAjax(questionnaireInfoService.insertQuestionnaireInfo(questionnaireInfo));
    }

    /**
     * 修改考核问卷
     */
    @RequiresPermissions("question:compose:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        QuestionnaireInfo questionnaireInfo = questionnaireInfoService.selectQuestionnaireInfoById(id);
        mmap.put("questionnaireInfo", questionnaireInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存考核问卷
     */
    @RequiresPermissions("question:compose:edit")
    @Log(title = "考核问卷", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(QuestionnaireInfo questionnaireInfo)
    {
        return toAjax(questionnaireInfoService.updateQuestionnaireInfo(questionnaireInfo));
    }

    /**
     * 删除考核问卷
     */
    @RequiresPermissions("question:compose:remove")
    @Log(title = "考核问卷", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(questionnaireInfoService.deleteQuestionnaireInfoByIds(ids));
    }
}